﻿// Copyright (C) 2009  Lex Li
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
using System.Drawing;
using System.Windows.Forms;

namespace Lextm.M8Pack.Components
{
    public class DrawingArea : Panel
    {
        public DrawingArea()
        {
            SetStyle(
                ControlStyles.UserPaint |
                ControlStyles.DoubleBuffer,true);
        }

        /// <summary>
        /// Override this method in subclasses for drawing purposes.
        /// </summary>
        protected virtual void OnDraw(Graphics graphics)
        {
            
        }
                
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT

                return cp;
            }
        }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            // Don't paint background
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            // Update the private member so we can use it in the OnDraw method
            Graphics graphics = e.Graphics;

            // Set the best settings possible (quality-wise)
            graphics.TextRenderingHint =
                System.Drawing.Text.TextRenderingHint.AntiAlias;
            graphics.InterpolationMode =
                System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
            graphics.PixelOffsetMode =
                System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
            graphics.SmoothingMode =
                System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            // Calls the OnDraw subclass method
            OnDraw(graphics);
        }
    }
}
